<template>
  <div>
    <!-- <h3>form组件</h3> -->
    <el-dialog
      :title="status.isAdd ? '管理员添加' : '管理员编辑'"
      :visible.sync="status.isshow"
      @closed="closed"
    >
      <el-form :model="form">
        {{ form }}
        <el-form-item label="所属角色" label-width="120px">
          <el-select v-model="form.roleid" placeholder="请选择">
            <el-option
              v-for="item in roleList"
              :key="item.id"
              :label="item.rolename"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="用户名" label-width="120px">
          <el-input v-model="form.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码" label-width="120px">
          <el-input v-model="form.password" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="状态" label-width="120px">
          <el-switch
            v-model="form.status"
            :active-value="1"
            :inactive-value="2"
          ></el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="isshow = status.isshow = false">取 消</el-button>
        <el-button type="primary" v-if="status.isAdd" @click="add"
          >添 加</el-button
        >
        <el-button type="primary" @click="update" v-else>修 改</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  reqrolelist,
  requseradd,
  requseredit,
  requserinfo,
} from "../../../http/api";
import { successAlert } from "../../../utils/alert";
import { mapGetters, mapActions } from "vuex";
export default {
  props: ["status", "list"],
  data() {
    return {
      // 获取角色列表数据
      roleList: [],
      // 添加接口初始化数据
      form: {
        roleid: "",
        username: "",
        password: "",
        status: 1,
      },
    };
  },
  methods: {
    ...mapActions({
      changeInfo: "changeInfo",
    }),
    empty() {
      this.form = {
        roleid: "",
        username: "",
        password: "",
        status: 1,
      };
    },
    add() {
      requseradd(this.form).then((res) => {
        if (res.data.code == 200) {
          // 弹成功
          successAlert(res.data.msg);
          // 清空
          this.empty();
          //   弹框消失
          this.status.isshow = false;
          // 列表重新渲染
          this.$emit("init");
        }
      });
    },

    // 点击编辑之后
    getOne(uid) {
      requserinfo({
        uid: uid,
      }).then((res) => {
        if (res.data.code == 200) {
          this.form = res.data.list;
          this.form.password = "";
        }
      });
    },
    // 点击修改
    update() {
      requseredit(this.form).then((res) => {
        if (res.data.code == 200) {
          // 弹成功
          successAlert(res.data.msg);
          // 修改的角色是自己正在登录着的
          if (this.form.uid == this.info.uid) {
            // 清空数据
            this.changeInfo({});
            // 退出登录
            this.$router.replace("/login");
            return;
          }
          // 清空
          this.empty();
          //   弹框消失
          this.status.isshow = false;
          // 列表重新渲染
          this.$emit("init");
        }
      });
    },
    closed() {
      if (!this.status.isAdd) {
        this.empty();
      }
    },
  },
  mounted() {
    // 取出角色列表数据
    reqrolelist().then((res) => {
      this.roleList = res.data.list;
    });
  },
  computed: {
    ...mapGetters(["info"]),
  },
};
</script>

<style>
</style>